﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightBase.Classes
{
	public class Media
	{
		public class Events
		{

			/// <summary>
			/// Handle media response
			/// </summary>
			/// <param name="sender"></param>
			/// <param name="e"></param>
			public static void ClientMedia_Completed(object sender, DownloadStringCompletedEventArgs e)
			{
				if (e.Error == null)
				{
					Classes.Xaml.Content.contentWidth contentWidth = Classes.Xaml.Content.contentWidth.Full;

					if (((Grid)((Page)Application.Current.RootVisual).ContentGrid.FindName("MainContent")).Width < Convert.ToDouble(600))
						contentWidth = Classes.Xaml.Content.contentWidth.Small;

					double contentGridHeight = ((Page)Application.Current.RootVisual).ContentGrid.ActualHeight;

					Canvas root = (Canvas)System.Windows.Markup.XamlReader.Load("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" + Classes.Xaml.Content.GetMainContentXaml(Classes.Xaml.Content.contentType.Media, "", e.Result.ToString(), ((Page)Application.Current.RootVisual).selectedColour, contentWidth) + "</Canvas>");
					UIElement realRoot = root.Children[0];
					root.Children.Remove(realRoot);
					((Page)Application.Current.RootVisual).ContentGrid.Children.Add(realRoot);

					((Grid)((Page)Application.Current.RootVisual).ContentGrid.FindName("MovieLayer")).Margin = new Thickness(141, 620, 0, 0);

					if (e.Result.ToString().IndexOf(".png") == -1 && e.Result.ToString().IndexOf(".jpg") == -1)
					{
						((MediaElement)((Page)Application.Current.RootVisual).ContentGrid.FindName("Movie")).MouseLeftButtonUp += new MouseButtonEventHandler(PlayStopMovie_MouseLeftButtonUp);
						((Grid)((Page)Application.Current.RootVisual).ContentGrid.FindName("PlayButton")).MouseLeftButtonUp += new MouseButtonEventHandler(PlayStopMovie_MouseLeftButtonUp);
					}
				}
			}

			public static void PlayStopMovie_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
			{
				if (((Page)Application.Current.RootVisual).movieIsLoaded)
				{
					if (((Page)Application.Current.RootVisual).movieIsPlaying)
					{
						((Grid)((Page)Application.Current.RootVisual).ContentGrid.FindName("Pause")).Visibility = Visibility.Collapsed;
						((Canvas)((Page)Application.Current.RootVisual).ContentGrid.FindName("Play")).Visibility = Visibility.Visible;

						((MediaElement)((Page)Application.Current.RootVisual).ContentGrid.FindName("Movie")).Pause();

						((Page)Application.Current.RootVisual).movieIsPlaying = false;
					}
					else
					{
						((Grid)((Page)Application.Current.RootVisual).ContentGrid.FindName("Pause")).Visibility = Visibility.Visible;
						((Canvas)((Page)Application.Current.RootVisual).ContentGrid.FindName("Play")).Visibility = Visibility.Collapsed;

						((MediaElement)((Page)Application.Current.RootVisual).ContentGrid.FindName("Movie")).Play();

						((Page)Application.Current.RootVisual).movieIsPlaying = true;
					}
				}
			}

			public static void Movie_Loaded(object sender, RoutedEventArgs e)
			{
				((Page)Application.Current.RootVisual).movieIsLoaded = true;
			}
		}
	}
}
